package leetcode;

/**
 * @author zhangjunfei
 * @date 2023/8/7 10:18
 */
public class L_240 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int x = 0, y = matrix[0].length - 1;
        int m = matrix.length, n = matrix[0].length;

        while (x < m && y >= 0) {
            if (matrix[x][y] == target) {
                return true;
            }
            if (matrix[x][y] > target) {
                y--;
            } else {
                x++;
            }
        }
        return false;
    }

    // 二分查找解法

    public boolean searchMatrix2(int[][] matrix, int target) {
        for (int[] ints : matrix) {
            int index = search(ints, target);
            if (index >= 0) {
                return true;
            }
        }
        return false;
    }

    public static int search(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                low = mid+1;
            }else{
                high = mid-1;
            }

        }
        return -1;
    }
}
